/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


/**
 * 2008.3.7
 * create Excel based on Jakarta POI HSSF
 */

public class ExcelWriter
{
    //header name
    private String[] _headerNames;
    //excel work book
    HSSFWorkbook _workBook;
    //excel sheet. This class only supports one sheet currently.
    HSSFSheet _sheet;
    //excel sheet name
    private String _excelTitle;
    //this is a temporary variable to record the excel sheet row number
    private int _rowNumber = 1;
    public ExcelWriter(String[] headerNames,String excelTitle)
    {
        //new a book
        _headerNames = headerNames;
        _workBook = new HSSFWorkbook();
        if(excelTitle == null)_excelTitle = "0";
        else _excelTitle = excelTitle;
        _sheet = _workBook.createSheet(_excelTitle);
        createHeader();
    }
    /**
     * add a new record into excel sheet.
     * this method is called in user's codes
     * @param excelRow ExcelRow
     */
    public void addRow(ExcelRow excelRow)
    {
        HSSFRow row = _sheet.createRow((short)_rowNumber);
        for(short i = 0;i < excelRow.getColumnNumber();i++)   
        {   
            HSSFCell cell = row.createCell((short) i);   
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);   
            cell.setCellValue(excelRow.getCell(i));   
        }   
        _rowNumber = _rowNumber+1;
    }
    
    protected void createHeader()
    {
        HSSFHeader header = _sheet.getHeader();
        header.setCenter(_excelTitle);
        HSSFRow headerRow = _sheet.createRow((short) 0);
        for(int i = 0;i < _headerNames.length;i++)   
         {   
             HSSFCell headerCell = headerRow.createCell((short) i);   
             headerCell.setEncoding(HSSFCell.ENCODING_UTF_16);   
             headerCell.setCellValue(_headerNames[i]);
         }   
    }
    

    
    public void export(OutputStream os) throws IOException
    {
       _workBook.write(os);
    }
    
    
    public static void main(String[] args) throws Exception
    {
        String[] headerNames = {"Id","loginName","Eail","Mobile","RegisterDate","LastLogonDate"};
        ExcelWriter ew = new ExcelWriter(headerNames,"Users");
        ExcelRow er = null;
        for(int i=0;i<5;i++)
        {
            er = new ExcelRow(headerNames.length);
            for(int j=0;j<er.getColumnNumber();j++)
            {
                er.setCellData(j,""+j);
            }
            ew.addRow(er);
        }
        FileOutputStream fos = new FileOutputStream("D:\\poitest.xls");
        ew.export(fos);
    }
    
}
